﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;

namespace LaKuaShop_TestDemo.Page
{
    /// <summary>
    /// CollectPage.xaml 的交互逻辑
    /// </summary>
    public partial class CollectPage : System.Windows.Controls.Page
    {
        public CollectPage()
        {
            InitializeComponent();
            InitCollectList();
        }
        private void InitCollectList()
        {
            List<Colgoods> ex = new List<Colgoods>();
            BLL.Collect Col = new BLL.Collect();
            BLL.Product Pro = new BLL.Product();
            CollectInfo collectInfo = Col.GetCollect(Common.User.account);
            for (int i = 0; i < collectInfo.PidList.Count; i++)
            {
                ProductInfo productInfo = Pro.ShowProductInfo(collectInfo.PidList[i]);
                byte[] img = Pro.ShowProductImg(collectInfo.PidList[i]);
                BitmapImage bmp = new BitmapImage();
                if (img == null)
                {
                    bmp = new BitmapImage(new Uri("/newlogo.png", UriKind.Relative));
                }
                else
                {
                    MemoryStream stream = new MemoryStream(img);
                    bmp.BeginInit();//初始化
                    bmp.StreamSource = stream;//设置源
                    bmp.EndInit();//初始化结束
                }
                string pricestr = "￥" + string.Format("{0:n2}", productInfo.Pprice);
                ex.Add(new Colgoods(bmp, pricestr, productInfo.Pname));
            }
            CollectList.ItemsSource = ex;
        }

        //删除
        private void Del_Button(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确认将该宝贝从收藏夹移出吗?", "确认提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                var mySelectedElement = (Colgoods)CollectList.SelectedItem;
                string selectedID = mySelectedElement.name;
                BLL.Product Pro = new BLL.Product();
                BLL.Collect Col = new BLL.Collect();
                List<ProductInfo> prolist = Pro.SearchProduct(selectedID);
                selectedID = prolist[0].Pid;
                Col.DelOneCollection(Common.User.account, selectedID);
                InitCollectList();
            }
        }
    }

    class Colgoods
    {
        public BitmapImage pic { get; set; }
        public string price { get; set; }
        public string name { get; set; }
        public Colgoods(BitmapImage pic, string price, string name)
        {
            this.pic = pic;
            this.price = price;
            this.name = name;
        }
    }
}
